package com.zrc.superframe_tbs

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.FileUtils
import com.zrc.superframe_net.room.download.DownloadInfo
import com.zrc.superframe_net.room.download.DownloadState
import com.zrc.superframe_net.common.download.NetDownloadCallBack
import com.zrc.superframe_net.common.download.NetDownloadManager
import kotlinx.android.synthetic.main.fragment_filereader.*

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:文件打开阅读页面
 */
class FileReaderFragment : Fragment(), NetDownloadCallBack, LifecycleOwner {
    var mRetrofitDownloadManager: NetDownloadManager? = null//下载管理 - 记得 ondestory
    var path: String = ""
    private var fileReaderView: FileReaderView? = null

    companion object {
        fun newInstance(path: String): FileReaderFragment {
            val args = Bundle()
            args.putString("path", path)
            val fragment = FileReaderFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view = inflater.inflate(R.layout.fragment_filereader, container, false)
        fileReaderView = view.findViewById(R.id.fileReaderView)
        return view
    }

    @SuppressLint("FragmentLiveDataObserve")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        TbsUtils.get().liveData.observe(this, Observer {
            when(it.state){
                TbsUtils.TbsInitMsg.TbsInitState.TBS_DOWNLOAD ->{
                    tv_progress.visibility=View.VISIBLE
                    tv_progress.text = "Tbs内核下载中... ${it.value}%"
                }

                TbsUtils.TbsInitMsg.TbsInitState.TBS_LOADING ->{
                    if (it.value==0){
                        tv_progress.visibility=View.VISIBLE
                        tv_progress.text = "Tbs内核加载失败..."
                    }else{
                        tv_progress.visibility=View.GONE
                        tv_progress.text = "Tbs内核加载成功..."
                        downFile()
                    }
                }
            }
        })
        initView()
    }

    private fun initView() {
        path = arguments?.getString("path") ?: ""
        mRetrofitDownloadManager = mRetrofitDownloadManager ?: NetDownloadManager(this)
    }

    private fun downFile(){
        val downInfo = mRetrofitDownloadManager?.createDownInfo(url = path,notNeedCache = true)//使用 NetDownloadManager 创建下载对象,内部有断点功能
        if (downInfo?.downState == DownloadState.FINISH.state) {//本地已经下载过了
            updateState(downInfo, DownloadState.FINISH)
        } else {
            mRetrofitDownloadManager?.down(downInfo!!)//点击按钮，开始更新
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        fileReaderView?.onStop()
        fileReaderView = null
        mRetrofitDownloadManager?.destory()
        mRetrofitDownloadManager = null
    }

    @SuppressLint("SetTextI18n")
    override fun updateProgress(info: DownloadInfo, progress: Int) {
        tv_progress.visibility = View.VISIBLE
        tv_progress.text = "文件加载中... ${progress}%"
    }

    @SuppressLint("SetTextI18n")
    override fun updateState(info: DownloadInfo, state: DownloadState) {
        if (TbsUtils.get().isOk()) {
            tv_progress.visibility = View.GONE
            fileReaderView?.show(FileUtils.getFileByPath(info.savePath))
        } else {
            tv_progress.visibility = View.VISIBLE
            tv_progress.text = "Tbs内核加载失败..."
        }
    }
}